package net.neiquan.applibrary.numberpicker.util;

import android.content.Context;


import com.alibaba.fastjson.JSON;

import net.neiquan.applibrary.numberpicker.entity.City;
import net.neiquan.applibrary.numberpicker.entity.Province;
import net.neiquan.applibrary.numberpicker.entity.Region;

import java.util.ArrayList;


/**
 * 
 * 城市代码
 * 
 * @author zd
 * 
 */
public class CityCodeUtil {




    private ArrayList<Province> provinces = new ArrayList<Province>();
	private ArrayList<City> citys = new ArrayList<City>();
	private ArrayList<Region> district = new ArrayList<Region>();

	/** 单例 */
	public static CityCodeUtil model;

    public ArrayList<Province> getProvinces() {
        return provinces;
    }


    public ArrayList<City> getCitys() {
        return citys;
    }

    public ArrayList<Region> getDistrict() {
        return district;
    }

	private CityCodeUtil() {
		
	}
	private CityCodeUtil(Context context) {
		provinces =(ArrayList<Province>) JSON.parseArray(FileUtils.readFileFromAssets(context, "allcity.json"), Province.class);
		for(Province p:provinces){
			citys.addAll(p.getCitys());
		}
		for(City p:citys){
			district.addAll(p.getRegions());
		}
	}
	/**
	 * 获取单例
	 * 
	 * @return
	 */
	public static CityCodeUtil getSingleton(Context context) {
		if (null == model) {
			model = new CityCodeUtil(context);
		}
		return model;
	}
    /**
     * 更具code 查询省级
     * @param code
     */
    public String getPrivenceByCode(String code){
        if (code==null){
            return "";
        }
        if("-1".equals(code)){
            return "---";
        }
        String str="";
        for (Province json:provinces){
            if (json.getCode()!=null && json.getCode().trim().equals(code.trim())){
                str= json.getName();
                break;
            }
        }
        return str;
    }
    /**
     * 更具code 查询市
     * @param code
     */
    public String getCityByCode(String code){
        if (code==null){
            return "";
        }
        if("-1".equals(code)){
            return "---";
        }
        String str="";
        for (City json:citys){
            if (json.getCode()!=null && json.getCode().trim().equals(code.trim())){
                str= json.getName();
                break;
            }
        }
        return str;
    }
    /**
     * 更具code 查询区
     * @param code
     */
    public String getDistrictByCode(String code){
        if (code==null){
            return "";
        }
        if("-1".equals(code)){
            return "---";
        }
        String str="";
        for (Region json:district){
            if (json.getCode()!=null && json.getCode().trim().equals(code.trim())){
                str= json.getName();
                break;
            }
        }
        return str;
    }
    /**
     * 更具省级查询code
     * @param name
     */
    public String getCodeByPrivence(String name){
        if (name==null){
            return "";
        }
        if("---".equals(name)){
            return "-1";
        }
        String str="";
        for (Province json:provinces){
            if (json.getName()!=null &&  name.trim().equals(json.getName().trim())){
                str= json.getCode();
                break;
            }
        }
        return str;
    }
    /**
     * 更具市查询code
     * @param name
     */
    public String getCodeByCity(String name){
        if (name==null){
            return "";
        }
        if("---".equals(name)){
            return "-1";
        }
        String str="";
        for (City json:citys){
            if (json.getName()!=null && name.trim().equals(json.getName().trim())){
                str= json.getCode();
                break;
            }
        }
        return str;
    }
    /**
     * 更具区查询code
     * @param name
     */
    public String getCodeByDistrict(String name){
        if (name==null){
            return "";
        }
        if("---".equals(name)){
            return "-1";
        }
        String str="";

        for (Region json:district){
            if (json.getName()!=null && name.trim().equals(json.getName().trim())){
                str= json.getCode();
                break;
            }
        }
        return str;
    }
}
